/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.common.utils;

import lombok.NonNull;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringSubstitutor;

import java.util.HashMap;
import java.util.Map;

/**
 * 占位符工具类
 *
 * @author zpy
 * @version 1.0.0
 */
public final class PlaceholderUtils {
    private final static String START_TAG = "{";
    private final static String END_TAG = "}";

    private final static String SYSTEM_NAME_TAG = "系统";
    private final static String ASSET_NAME_TAG = "资产对象";
    private final static String ASSET_SCORE_TAG = "对象得分";
    private final static String SECURITY_THREAT_TAG = "安全威胁";

    private final String systemName;
    private final String assetName;
    private final String assetScore;
    private final String securityThreat;

    private PlaceholderUtils(Builder builder) {
        this.systemName = builder.systemName;
        this.assetName = builder.assetName;
        this.assetScore = builder.assetScore;
        this.securityThreat = builder.securityThreat;
    }

    public static Builder builder() {
        return new Builder();
    }

    public String renderContent(@NonNull String template) {
        if (StringUtils.isEmpty(template)) {
            return StringUtils.EMPTY;
        }
        StringSubstitutor sub = new StringSubstitutor(getData(), START_TAG, END_TAG);
        return sub.replace(template);
    }

    private Map<String, String> getData() {
        Map<String, String> templateData = new HashMap<>(2);
        if (StringUtils.isNotEmpty(this.systemName)) {
            templateData.put(SYSTEM_NAME_TAG, this.systemName);
        }
        if (StringUtils.isNotEmpty(this.assetName)) {
            templateData.put(ASSET_NAME_TAG, this.assetName);
        }
        if (StringUtils.isNotEmpty(this.assetScore)) {
            templateData.put(ASSET_SCORE_TAG, this.assetScore);
        }
        if (StringUtils.isNotEmpty(this.securityThreat)) {
            templateData.put(SECURITY_THREAT_TAG, this.securityThreat);
        }

        return templateData;
    }

    public static final class Builder {
        private String systemName;
        private String assetName;
        private String assetScore;
        private String securityThreat;

        private Builder() {
        }

        public Builder withSystemName(String systemName) {
            this.systemName = systemName;
            return this;
        }

        public Builder withAssetName(String assetName) {
            this.assetName = assetName;
            return this;
        }

        public Builder withAssetScore(String assetScore){
            this.assetScore = assetScore;
            return this;
        }

        public Builder withSecurityThreat(String securityThreat){
            this.securityThreat = securityThreat;
            return this;
        }

        public PlaceholderUtils build() {
            return new PlaceholderUtils(this);
        }
    }
}